using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Security.Cryptography;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Infrastructure.Tools;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
/// <summary>
/// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
/// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
/// </summary>
	internal class VipsEF
	{
		public VipsEF(DbContext dbContext)
		{
			vipsServerEF = new VipsServerEF(dbContext);
            vipDiscountsServerEF = new VipDiscountsServerEF(dbContext);
            vipDeductionsServerEF = new VipDeductionsServerEF(dbContext);
            discountsServerEF = new DiscountsServerEF(dbContext);
            deductionsServerEF = new DeductionsServerEF(dbContext);
            sysVipPackagesServerEF = new SysVipPackagesServerEF(dbContext);
            accountServerEF = new SysAccountsServerEF(dbContext);
            sysCustomersServerEF = new SysCustomersServerEF(dbContext);
            offlineOrdersServerEF = new OfflineOrdersServerEF(dbContext);
            sysCarShopsServerEF = new SysCarShopsServerEF(dbContext);
            sysProductsServerEF = new SysProductsServerEF(dbContext);
            employeesServerEF = new EmployeesServerEF(dbContext);
        }
		public VipsServerEF vipsServerEF { get; set; }
        public VipDiscountsServerEF vipDiscountsServerEF { get; set; }
        public VipDeductionsServerEF vipDeductionsServerEF { get; set; }
        public DiscountsServerEF discountsServerEF { get; set; }
        public DeductionsServerEF deductionsServerEF { get; set; }
        public SysVipPackagesServerEF sysVipPackagesServerEF { get; set; }
        public SysAccountsServerEF accountServerEF { get; set; }
        public SysCustomersServerEF sysCustomersServerEF { get; set; }
        public OfflineOrdersServerEF offlineOrdersServerEF { get; set; }
        public SysCarShopsServerEF sysCarShopsServerEF { get; set; }
        public SysProductsServerEF sysProductsServerEF { get; set; }
        public EmployeesServerEF employeesServerEF { get; set; }
        public CustomerOrdersServerEF customerOrdersServerEF { get; set; }
    }
	[InjectClass]
	public class VipsServer : IVipsServer
	{
		private readonly VipsEF _vipsEF;//包含所有所需仓库表实现的对象
		// 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
		public VipsServer(DbContext dbContext)
		{
			_vipsEF = new VipsEF(dbContext);
		}
        public async Task<ResponseApiDto> AddAsync(RequestVipsDto dto)
        {
            dto.cid = await _vipsEF.sysCarShopsServerEF.Query(d => d.CsAccount == dto.cid).Select(d => d.CsId).FirstOrDefaultAsync();
            return ResponseApiDto.Create(await _vipsEF.vipsServerEF.AddAsync(dto.ToCreateEntity()));
        }
        public async Task<ResponseApiDto> DeleteAsync(RequestVipsDto dto)
        {
            return ResponseApiDto.Create(await _vipsEF.vipsServerEF.DeleteAsync(dto.ToDeleteEntity()));
        }
        public async Task<ResponseApiDto> UpdateAsync(RequestVipsDto dto)
        {
            return ResponseApiDto.Create(await _vipsEF.vipsServerEF.UpdateAllProperNotNullAsync(dto.ToUpdateEntity()));
        }
        public async Task<ResponseApiDto> GetSelectAsync(string carShopId)
        {
            return ResponseApiDto.Success("数据已获取",await _vipsEF.vipsServerEF.Query(d=>d.VCsId==carShopId).Select(d=>new ResponseSelectDto { id=d.VId,name=d.VName}).ToListAsync());
        }
        

        public async Task<ResponseApiDto> GetVip(string uid)
        {
            string cid = await _vipsEF.employeesServerEF.Query(d => d.EAccountId == uid).Select(d => d.ECarShopId).FirstOrDefaultAsync();
            List<ResponseVipsDto> data = new List<ResponseVipsDto>();
            List<Vips> vipss = await _vipsEF.vipsServerEF.Query(d=>d.VCsId== cid).ToListAsync();
            foreach (var vips in vipss)
            {
                data.Add(vips.ToResponseDto());
            }
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { data = data });
        }
        /// <summary>
        /// PC获取所有vip用户
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseApiDto> Getemploy_Vip(int page, int limit, string key, string uid)
        {
            string cid = await _vipsEF.employeesServerEF.Query(d => d.EAccountId == uid).Select(d => d.ECarShopId).FirstOrDefaultAsync();
            int total = 0;
            key = key ?? "";
            List<SysAccounts> uase = await _vipsEF.accountServerEF.Pagination1(d => d.AAccount.Contains(key)&&d.ARole== "user", false, d => d.ACreateTime, out total, page, limit).ToListAsync();
            if (!uase.Any()) {
                return ResponseApiDto.Fail();
            }
            List<object> data = new List<object>();
            foreach (var item in uase)
            {
                string vid= await _vipsEF.sysCustomersServerEF.Query(d => d.CAccount == item.AId).Select(d => d.CVip).SingleOrDefaultAsync();
                if (vid != null)
                {
                    data.Add(new
                    {
                        id = item.AId,
                        name = item.AName,
                        phasr = item.AAccount,
                        vipName = await _vipsEF.vipsServerEF.Query(d => d.VId == vid && d.VCsId == cid).Select(d => d.VName).SingleOrDefaultAsync(),
                        purchasetime = await _vipsEF.offlineOrdersServerEF.Query(d => d.OoAccountId == item.AId).Select(d => d.OoUpdateAt).SingleOrDefaultAsync(),

                    });
                }
            }
            total= data.Count;
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });
        }
        /// <summary>
        /// 查询VIP （carShop）
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseApiDto> Get_Vip_carShop(int page, int limit, string key, string uid)
        {
            string cid = await _vipsEF.sysCarShopsServerEF.Query(d => d.CsAccount == uid).Select(d => d.CsId).FirstOrDefaultAsync();
            int total = 0;
            key = key ?? "";
            List<Vips> vipss = await _vipsEF.vipsServerEF.Pagination1(d => d.VName.Contains(key)&&d.VCsId==cid, false, d => d.VLevel, out total, page, limit).ToListAsync();
            List<object> data = new List<object>();
            foreach (var employees in vipss)
            {
                string pid = await _vipsEF.sysVipPackagesServerEF.Query(d => d.VpVipId == employees.VId).Select(d => d.VpProductId).FirstOrDefaultAsync();
                decimal pdid = await _vipsEF.sysProductsServerEF.Query(d => d.PId == pid).Select(d => d.PPrice).FirstOrDefaultAsync();
                data.Add(new
                {
                    id = employees.VId,
                    name = employees.VName,
                    color = employees.VpColor,
                    price=pdid,
                    description = employees.VDescription,
                    foever = employees.VFoeverDiscount,
                    level=employees.VLevel,
                    time=employees.VCreateTime
                });
            }
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });

        }
        /// <summary>
        /// PC单个获取vip用户详细
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseApiDto> Get_Vip_off(string id,string uid)
        {
            string cid = await _vipsEF.employeesServerEF.Query(d => d.EAccountId == uid).Select(d => d.ECarShopId).FirstOrDefaultAsync();
            var vid = await _vipsEF.sysCustomersServerEF.Query(d => d.CPhoneNumber == id).Select(d => d.CVip).SingleOrDefaultAsync();
            var vipss = await _vipsEF.vipsServerEF.Query(d=>d.VId== vid&&d.VCsId==cid).Select(d=>d.ToResponseDto()).ToListAsync();
            return ResponseApiDto.Success("数据已获取", vipss);
        }
        /// <summary>
        /// 查询创建VIP订单
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseApiDto> Get_Vip_offtrrezw(string vipId, string cid,string email)
        {
            cid = await _vipsEF.employeesServerEF.Query(d => d.EAccountId == cid).Select(d => d.ECarShopId).FirstOrDefaultAsync();
            //获取VIP名称
            var vips = await _vipsEF.vipsServerEF.Query(d => d.VId == vipId).Select(d => new
            {
                name=d.VName,
                dis=d.VFoeverDiscount
            }).SingleOrDefaultAsync();
            //获取vip的会员有效期
            var imt = await _vipsEF.sysVipPackagesServerEF.Query(d => d.VpVipId == vipId).Select(d => new
            {
                djimt = DateTime.Now.AddYears(d.VpYear).AddMonths(d.VpMonth),
            }).SingleOrDefaultAsync();
            //获取积分
            var point = await _vipsEF.sysCustomersServerEF.Query(d => d.CPhoneNumber == email).Select(d => d.CPoints).SingleOrDefaultAsync();
            //获取产品价格
            var prid = await _vipsEF.sysVipPackagesServerEF.Query(d => d.VpVipId == vipId).Select(d => d.VpProductId).SingleOrDefaultAsync();
            var pric = await _vipsEF.sysProductsServerEF.Query(d => d.PId == prid).Select(d => d.PPrice).SingleAsync();
            List<object> data = new List<object>();
            data.Add(new{
                vipname= vips.name,
                vipdis= vips.dis,
                vipimt=imt.djimt,
                pric=pric,
                point=point,
            });
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto {  data = data });
        }
    }
}
